package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TShortanswer;
import com.ruoyi.project.system.service.ITShortanswerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 简答题库Controller
 * 
 * @author ruoyi
 * @date 2020-05-09
 */
@RestController
@RequestMapping("/system/shortanswer")
public class TShortanswerController extends BaseController
{
    @Autowired
    private ITShortanswerService tShortanswerService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询简答题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:shortanswer:list')")
    @GetMapping("/list")
    public TableDataInfo list(TShortanswer tShortanswer)
    {
        startPage();
        List<TShortanswer> list = tShortanswerService.selectTShortanswerList(tShortanswer);
        return getDataTable(list);
    }

    /**
     * 导出简答题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:shortanswer:export')")
    @Log(title = "简答题库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TShortanswer tShortanswer)
    {
        List<TShortanswer> list = tShortanswerService.selectTShortanswerList(tShortanswer);
        ExcelUtil<TShortanswer> util = new ExcelUtil<TShortanswer>(TShortanswer.class);
        return util.exportExcel(list, "shortanswer");
    }

    /**
     * 获取简答题库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:shortanswer:query')")
    @GetMapping(value = "/{shortanswerId}")
    public AjaxResult getInfo(@PathVariable("shortanswerId") Integer shortanswerId)
    {
        return AjaxResult.success(tShortanswerService.selectTShortanswerById(shortanswerId));
    }

    /**
     * 新增简答题库
     */
    @PreAuthorize("@ss.hasPermi('system:shortanswer:add')")
    @Log(title = "简答题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TShortanswer tShortanswer)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        tShortanswer.setUserId(user.getUserId().intValue());
        tShortanswerService.insertTShortanswer(tShortanswer);
        System.out.println("tShortanswer = " + tShortanswer);
        return toAjax( tShortanswerService.addMiddle(tShortanswer.getCourseId(),tShortanswer.getShortanswerId()));
    }

    /**
     * 修改简答题库
     */
    @PreAuthorize("@ss.hasPermi('system:shortanswer:edit')")
    @Log(title = "简答题库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TShortanswer tShortanswer)
    {
        return toAjax(tShortanswerService.updateTShortanswer(tShortanswer));
    }

    /**
     * 删除简答题库
     */
    @PreAuthorize("@ss.hasPermi('system:shortanswer:remove')")
    @Log(title = "简答题库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shortanswerIds}")
    public AjaxResult remove(@PathVariable Integer[] shortanswerIds)
    {
        return toAjax(tShortanswerService.deleteTShortanswerByIds(shortanswerIds));
    }
}
